package com.example.index.gesture

import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.index.R
import com.example.index.extension.ev
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_gesture_base_detect.*

class GestureBaseDetectActivity : AppCompatActivity() {

    val TAG = "GestureBaseDetectActivity"


    lateinit var gestureDetector: GestureDetector


    var gestureDetectorListener = object : GestureDetector.SimpleOnGestureListener() {


        /**
         *常规双击判定，快速点击两下
         */
        override fun onDoubleTap(e: MotionEvent?): Boolean {
            Log.i(TAG, "onDoubleTap: ")
//            Toasty.info(this@GestureBaseDetectActivity, "onDoubleTap", Toast.LENGTH_SHORT, true).show()
            return false
        }


        /**
         * 如果对双击的响应更精细，比如必须第二下按下就认为是是完成双击，可以在这个方法这里响应按下的手势，
         * 这个回调是第二次down 开始的事件
         */
        override fun onDoubleTapEvent(e: MotionEvent?): Boolean {
            Log.i(TAG, "onDoubleTapEvent: ")
            if (doubleTapWhenSecondDown()) {
                if (e?.actionMasked == MotionEvent.ACTION_DOWN)
                    Toasty.info(this@GestureBaseDetectActivity, "第二次按下触发双击").show()
            } else if (e?.actionMasked == MotionEvent.ACTION_UP)
                Toasty.info(this@GestureBaseDetectActivity, "第二次抬起触发双击").show()

            return false
        }


        override fun onContextClick(e: MotionEvent?): Boolean {
            Log.i(TAG, "onContextClick: ")
            return super.onContextClick(e)
        }

        override fun onDown(e: MotionEvent?): Boolean {
            Log.i(TAG, "onDown: ")
            return true
        }


        override fun onLongPress(e: MotionEvent?) {
            Log.i(TAG, "onLongPress: ")
            super.onLongPress(e)
        }


        /**
         * 如果对单击的响应更精细，比如按下就认为是单击，可以在这个方法这里响应按下的手势
         */
        override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
            Log.i(TAG, "onSingleTapConfirmed: ")
            if (singleTapWhenDown()) {
                if (e?.actionMasked == MotionEvent.ACTION_DOWN) {
                    Toasty.info(this@GestureBaseDetectActivity, "第一次按下触发单击").show()
                }
            } else
                if (e?.actionMasked == MotionEvent.ACTION_UP)
                    Toasty.info(this@GestureBaseDetectActivity, "第一次抬手触发单击").show()

            return super.onSingleTapConfirmed(e)
        }

        /**
         * 常规的点击，即按下后松手即 单击 事件
         */
        override fun onSingleTapUp(e: MotionEvent?): Boolean {
            Log.i(TAG, "onSingleTapUp: ")
            return false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gesture_base_detect)
        gestureDetector = GestureDetector(this, gestureDetectorListener)

        gestureView.setOnTouchListener { v, event ->

            return@setOnTouchListener gestureDetector.onTouchEvent(event)
        }

    }

//
//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        Log.i(TAG, "onTouchEvent: ${event?.ev()}")
//        return super.onTouchEvent(event)
//    }


    fun singleTapWhenDown() = singleTapRadioGroup.checkedRadioButtonId == R.id.RbsingleTapWhenDown

    fun doubleTapWhenSecondDown() = doubleTapRadioGroup.checkedRadioButtonId == R.id.RbdoubleTapWhenDown
}
